--Round Stuff
--When the round is reset/server has started
function On_Round_Init( )
	--This just initializes each round
	if ( GetConVar("db_warmup_each_round"):GetInt( ) == 1 or GetConVar("db_warmup_each_round"):GetInt( ) == 0 and  RoundNumber < 2) then
		Message_All( "Warm Up beginning", HUD_PRINTCENTER )
		Begin_Time( GetConVar("db_time_warmup"):GetInt(), 0, false, On_Warm_Up_Delay_Over )
		RoundState = 1
	else
		Clean_Map_Up( )
		Teams_Balance( )
		Spawn_All_Players( )
		Freeze_All_Players( )
		Message_All( "Round beginning", HUD_PRINTCENTER )
		Begin_Time( GetConVar("db_time_round"):GetInt(), 5, true, On_Round_Delay_Over )
		RoundState = 1.5
	end
end

--this is what happens when the delay finishes before the Warm Up
function On_Warm_Up_Delay_Over( )
	Spawn_Balls()
end

--everything that happens at the end of the Warm Up
function On_Warm_Up_Over( )
	Clean_Map_Up( )
	Teams_Balance( )
	Spawn_All_Players( )
	Freeze_All_Players( )
	Message_All( "Round beginning", HUD_PRINTCENTER )
	Begin_Time( GetConVar("db_time_round"):GetInt(), 5, true, On_Round_Delay_Over )
    RoundState = 1.5
end

--this is what happens when the delay finishes before the main round
function On_Round_Delay_Over( )
	Spawn_Balls( )
	Thaw_All_Players( )
	Message_All( "GO", HUD_PRINTCENTER )
	RoundState = 2
end

--everything that happens at the end of the round
function On_Round_Over( prematureEnd )
	--prematureEnd = the round ended because a team ran out of players
	local winner = 0
	if ( prematureEnd ) then
		--Check which team ran out of players
		if ( team.NumPlayers( TEAM_COMBINE ) == 0 and team.NumPlayers( TEAM_LAMBDA ) > 0) then
			--LAMBDA WINS
			winner = TEAM_LAMBDA
		elseif( team.NumPlayers( TEAM_LAMBDA ) == 0 and team.NumPlayers( TEAM_COMBINE ) > 0) then
			--COMBINE WINS
			winner = TEAM_COMBINE
		elseif( team.NumPlayers( TEAM_LAMBDA ) == 0 and team.NumPlayers( TEAM_COMBINE ) == 0) then
			winner = -1
		end
		--Stop the round
		timer.Destroy( "time" )
		timer.Destroy( "delay" )
		DelayTime = 0
		RoundTime = 0
	else
		--Check which team has more players
		if ( team.NumPlayers( TEAM_COMBINE ) > team.NumPlayers( TEAM_LAMBDA ) ) then
			--COMBINE WINS
			winner = TEAM_COMBINE
		elseif ( team.NumPlayers( TEAM_LAMBDA ) > team.NumPlayers( TEAM_COMBINE ) ) then
			--LAMBDA WINS
			winner = TEAM_LAMBDA
		else
			winner = -1
		end
	end
	--End of round crap, display winners ect.
	if ( winner != -1 ) then
		Message_All( "Team " .. team.GetName( winner ) .. " won!", HUD_PRINTCENTER )
		team.SetScore(winner, team.GetScore(winner) + 1)
	else
		Message_All( "There was a tie", HUD_PRINTCENTER )
	end
	umsg.Start( "EndOfRound" )
		umsg.String( GetConVar("db_time_score_show"):GetInt( ) )
		if ( winner != -1 ) then
			umsg.String( team.GetName( winner ) )
		else
			umsg.String( "None" )
		end
	umsg.End( )
	Begin_Time( GetConVar("db_time_score_show"):GetInt( ), 0, false )
	RoundState = 3
end

function On_Scores_Over()
	if ( GetConVar("db_rounds_per_map"):GetInt( ) == RoundNumber and GetConVar("db_rounds_per_map"):GetInt( ) > 0 ) then
		Begin_Time( GetConVar("db_time_map_vote"):GetInt( ), 0, false )
		RoundState = RoundState + 1
		umsg.Start( "vote_for_next_map", pl )
			umsg.String( string.Implode(",", GAMEMODE.maps) )
		umsg.End( )
	else
		RoundNumber = RoundNumber + 1
		RoundState = 0
	end
end

function On_Vote_Map_Over( )
	--Tally up the votes and swtich the level.
	local map = 1
	for k, v in pairs( GAMEMODE.mapVotes ) do
		if (v > GAMEMODE.mapVotes[map]) then
			map = k
		end
	end
	Save_All_Player_Data( )
	local nextmap = string.Explode(".",GAMEMODE.maps[map])
	print("The map is " .. GAMEMODE.maps[map])
	RunConsoleCommand("changelevel", "" .. nextmap[1])
end


-------Everything below deals with timing the rounds

--Begin time starts the round timer which will run update time untl it expires.
function Begin_Time( timeLimit, delay, countdown, delayOver )
	TimeDone = false
	if ( delay == 0 and timeLimit > 0 and delayOver == nil) then
		RoundTime = timeLimit
		timer.Create( "time", 1, timeLimit, Update_Time )
	elseif ( delay == 0 and timeLimit > 0 and delayOver != nil) then
		--This will run the specified delay over function, even if there was no delay.
		delayOver()
		RoundTime = timeLimit
		timer.Create( "time", 1, timeLimit, Update_Time )	
	elseif ( delay > 0 ) then
		RoundTime = timeLimit
		DelayTime = delay
		timer.Create( "delay", 1, delay, Update_Delay,  delayOver, countdown )
	end
end


function Update_Delay( delayOver, countdown )
	DelayTime = DelayTime - 1
	if ( countdown ) then
		Message_All( DelayTime, HUD_PRINTCENTER )
	end

	if ( DelayTime == 0 ) then
		Begin_Time( RoundTime, 0 )
		--Run the function(s) that were specified in the third arg of Begin_Time
		if ( delayOver != nil ) then
			delayOver()
		end
	end
end

function Update_Time( )
	RoundTime = RoundTime - 1
	if ( RoundTime < 1 ) then
		TimeDone = true
	end
end
